package com.sword.controller.finance;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.finance.Receivables;
import com.sword.pojo.stock.BacksaleDetail;
import com.sword.service.finance.ReceivablesService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/finance")
public class ReceivablesController {
    @Autowired
    private ReceivablesService ReceivablesService;

    /*
     * 商品分类渲染
     */
    @RequestMapping("/receivables")
    public String receivables() { return "views/finance/receivables"; }

    /**
     * 查询功能
     */
    @RequestMapping("/queryReceivables")
    @ResponseBody
    public PageResult queryReceivables(@RequestBody QueryPageBean queryPageBean) {
        try {
            return ReceivablesService.queryReceivables(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_RECEIVABLES_FAIL, null, null);
        }
    }
    /*
     * 添加功能
     */
    @ResponseBody
    @RequestMapping("/addReceivables")
    public Result addReceivables(@RequestBody Receivables receivables) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            receivables.setCreateid(userId);
            receivables.setStatus(0);
            // 判断是否添加成功
            Integer flag = ReceivablesService.addReceivables(receivables);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_RECEIVABLES_SUCCESS, null);
    }
    @ResponseBody
    @RequestMapping("editReceivables")
    public Result editReceivables(@RequestBody Receivables receivables) {
        try {
            Integer flag = ReceivablesService.editReceivables(receivables);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLES_SUCCESS, null);
    }
    /**
     * 删除商品总分类
     */

    @ResponseBody
    @RequestMapping("deleteReceivables")
    public Result deleteReceivables(Integer id) {
        try {
            Integer flag = ReceivablesService.deleteReceivables(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_RECEIVABLES_SUCCESS, null);
    }
    @ResponseBody
    @RequestMapping("examineBuyorderDetail")
    public Result examineBuyorderDetail(@RequestBody Receivables receivables) {
        try {
            Integer flag = ReceivablesService.examineBuyorderDetail(receivables);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLES_SUCCESS, null);
    }
    /**
     * 弃审退货明细
     */
    @ResponseBody
    @RequestMapping("unexamineBuyorderDetail")
    public Result unexamineBuyorderDetail(@RequestBody Receivables receivables) {
        try {
            Integer flag = ReceivablesService.unexamineBuyorderDetail(receivables);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLES_SUCCESS, null);
    }

}
